package com.hngm.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.hngm.bean.Company;
import com.hngm.bean.Product;
import com.hngm.util.DBManager;
import com.hngm.util.QueryDataResult;
import com.hngm.util.StringUtil;
import com.jspsmart.upload.Request;

/**
 * 企业数据访问层
 * */
public class CompanyDao {
   public Company queryCompanyInfo(){
	   Company company = new Company();
	   //定义查询sql语句
	   String sql = "select * from company limit 1";
	   //调用DBManager查询数据库
	   QueryDataResult qdr = DBManager.executeQuery(sql);
	   //从qdr中获取resultSet结果集
	   ResultSet rs = qdr.getRs();
	   try {
		   //遍历结果集rs
		while(rs.next()){
			//每次获取一行企业信息
			int id = rs.getInt("id");
			String name = rs.getString("name");
			String address = rs.getString("address");
			String websiteurl = rs.getString("websiteurl");
			String shortintroduction = rs.getString("shortintroduction");
			String introduction = rs.getString("introduction");
			String hotline = rs.getString("hotline");
			String phone = rs.getString("phone");
			String mailbox = rs.getString("mailbox");
			String culture = rs.getString("culture");
			String creationtime = rs.getString("creationtime");
			String updatetime = rs.getString("updatetime");
			company = new Company(id, name, address, websiteurl, shortintroduction, introduction, hotline, phone, mailbox, culture, creationtime, updatetime);
		   }
	} catch (SQLException e) {
		DBManager.closedAll(qdr.getConn(), qdr.getSt(), qdr.getRs());
		// TODO Auto-generated catch block
		e.printStackTrace();
	} finally{
		DBManager.closedAll(qdr.getConn(), qdr.getSt(), qdr.getRs());
	}
	   return company;
   }

public ArrayList<Company> queryCompanyListByParm(String companyname) {
	        ArrayList<Company> cList = new ArrayList<Company>();
	        // 定义sql语句
			// String sql = String.format("select * from product limit %d , %d",
			// beginIndex,limit);
			// 将传过来的查询条件作为where条件：拼接到sql语句中
			StringBuffer sb = new StringBuffer("select * from company where 1=1");

			// 拼接查询条件
			if (StringUtil.isNotNullorEmpty(companyname)) {
				// sb.append(String.format(" and name like '%s%s%s'",
				// "%",productname,"%"));
				sb.append(String.format(" and name like '%s'", "%" + companyname + "%"));
			}
			
			// 调用DBManager
			QueryDataResult qdr = DBManager.executeQuery(sb.toString());
			// 在qdr中获取resultSet结果集
			ResultSet rs = qdr.getRs();

			try {
				// 遍历rs结果集
				while (rs.next()) {
					//每次获取一行企业信息
					int id = rs.getInt("id");
					String name = rs.getString("name");
					String address = rs.getString("address");
					String websiteurl = rs.getString("websiteurl");
					String shortintroduction = rs.getString("shortintroduction");
					String introduction = rs.getString("introduction");
					String hotline = rs.getString("hotline");
					String phone = rs.getString("phone");
					String mailbox = rs.getString("mailbox");
					String culture = rs.getString("culture");
					String creationtime = rs.getString("creationtime");
					String updatetime = rs.getString("updatetime");
					Company company = new Company(id, name, address, websiteurl, shortintroduction, introduction, hotline, phone, mailbox, culture, creationtime, updatetime);
				   cList.add(company);
				}

			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				DBManager.closedAll(qdr.getConn(), qdr.getSt(), qdr.getRs());
			}
	return cList;
}

public int queyrCompanyListByParamCount() {
	// 定义sql语句
//	String sql = "select count(1) as count from product";
	// 将传过来的查询条件作为where条件：拼接到sql语句中
//	StringBuffer sb = new StringBuffer("select count(1) as count from company where 1=1");
	String sql = "select count(1) as count from company";

	// 调用DBManager
	QueryDataResult qdr = DBManager.executeQuery(sql);
	// 在qdr中获取resultSet结果集
	ResultSet rs = qdr.getRs();

	try {
		// 遍历rs结果集
		while (rs.next()) {
			return rs.getInt("count");
		}

	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} finally {
		DBManager.closedAll(qdr.getConn(), qdr.getSt(), qdr.getRs());
	}
	return 0;
}

public int editCompany(Company p) {
	String sql = String.format("update company set "
			+ "name='%s',"
			+ "address='%s',"
			+ "websiteurl='%s',"
			+ "shortintroduction='%s',"
			+ "introduction='%s',"
			+ "hotline='%s',"
			+ "phone='%s',"
			+ "mailbox='%s',"
			+ "culture='%s' "
			+ "where id = %d", 
			p.getName(),
			p.getAddress(),
			p.getWebsiteurl(),
			p.getShortintroduction(),
			p.getIntroduction(),
			p.getHotline(),
			p.getPhone(),
			p.getMailbox(),
			p.getCulture(),
			p.getId());
	return DBManager.executeUpdate(sql);
}
}
